package com.jblv.frame.user.service.bll.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jblv.frame.common.enums.ExceptionEnum;
import com.jblv.frame.common.exception.FrameException;
import com.jblv.frame.common.type.BooleanType;
import com.jblv.frame.common.type.StatusType;
import com.jblv.frame.common.utils.UUIDGenerator;
import com.jblv.frame.user.api.bll.ISysDeptBLL;
import com.jblv.frame.user.api.dto.SysDeptDTO;
import com.jblv.frame.user.api.entity.SysDept;
import com.jblv.frame.user.api.params.SysDeptListParams;
import com.jblv.frame.user.api.service.ISysDeptService;
import com.jblv.frame.user.api.service.ISysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 部门业务逻辑类
 * @Author   yangtao
 * @Create   2020-05-26
 **/
@Component
@Service
public class SysDeptBLLImpl implements ISysDeptBLL {

    @Autowired
    private ISysDeptService sysDeptService;

    private static Logger logger = LoggerFactory.getLogger(SysDeptBLLImpl.class);


    @Override
    public SysDept get(String id) {

//        String key = BaseCacheUtils.getBeanCacheKeyById(id);
        SysDept bean = null;//BaseCacheUtils.get(key);
        if (bean != null) {
            return bean;
        }
        EntityWrapper<SysDept> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        wrapper.eq("DEPT_ID",id);
        bean = sysDeptService.selectOne(wrapper);
        if (bean != null) {
            logger.info("add cache");
            //addCache(bean);
        }

        return bean;
    }


    @Override
    public String save(String creator, SysDept sysDept) {
        if (sysDept == null) {
            return null;
        }
        if (StringUtils.isEmpty(sysDept.getDeptId())) {
            sysDept.setDeptId(UUIDGenerator.getUUID());
        }

        sysDept.setCreator(creator);
        sysDept.setCreateTime(System.currentTimeMillis());
        sysDept.setDeleteFlag(BooleanType.FALSE.getCode());

        boolean save = false;
        try {
            save = sysDeptService.insert(sysDept);
        }catch (FrameException e){
            logger.error("插入错误:{}",e.getMessage());
        }
        if (save) {
            //addCache(sysDept);
            return sysDept.getDeptId();
        }

        return null;
    }

    @Override
    public boolean update(String operator, SysDept sysDept) {
        SysDept sysDeptCheck = get(sysDept.getDeptId());
        if (sysDeptCheck == null) {
            throw new FrameException(ExceptionEnum.PARAM_ERROR);
        }
        if (sysDept != null) {

            sysDept.setUpdater(operator);
            sysDept.setUpdateTime(System.currentTimeMillis());

            boolean flag = sysDeptService.updateById(sysDept);
            if (flag) {
                //SysDept newBean = sysDeptService.selectById(sysDept.getId());
                //updateCache(oldUserInfo, newBean);
                return true;
            }
        }
        return false;
    }



    @Override
    public boolean remove(String operator, String id) {

        if (StringUtils.isEmpty(id)) {
            return false;
        }
        SysDept sysDept = get(id);
        if (sysDept != null) {
            SysDept sysDeptUpdate = new SysDept();
            sysDeptUpdate.setDeleteFlag(BooleanType.TRUE.getCode());
            sysDeptUpdate.setUpdater(operator);
            sysDeptUpdate.setDeptId(id);
            Boolean flag = sysDeptService.updateById(sysDeptUpdate);
            if (flag) {
                //removeCache(bayonetInfo);
                return true;
            }
        }
        return false;
    }

    @Override
    public Page<SysDept> getList(SysDeptListParams params) {
        EntityWrapper<SysDept> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        if (params.getCreateTimeBegin() != null) {
            wrapper.ge("CREATE_TIME", params.getCreateTimeBegin());
        }
        if (params.getCreateTimeEnd() != null) {
            wrapper.lt("CREATE_TIME", params.getCreateTimeEnd());
        }

        if (params.getName() != null){
            wrapper.like("NAME", params.getName());
        }

        wrapper.orderBy("CREATE_TIME", false);


        Page<SysDept> page = sysDeptService.selectPage(new Page<SysDept>(params.getPage(), params.getLimit()), wrapper);
        return page;
    }

    @Override
    public List<SysDept> getList() {
        EntityWrapper<SysDept> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        wrapper.orderBy("CREATE_TIME", false);

        List<SysDept> sysDepts = sysDeptService.selectList(wrapper);
        return sysDepts;
    }


    @Override
    public List<SysDept> getTreeList() {
        List<SysDept> returnDTO = new ArrayList();

        getTree("0",returnDTO);

        return returnDTO;
    }

    public void getTree(String parentId, List<SysDept> sysDepts){
        List<SysDept> listByParentId = getListByParentId(parentId);

        if(listByParentId.size()==0){
             return;
        }

        for (SysDept sysDept : listByParentId) {
            sysDepts.add(sysDept);

            getTree(sysDept.getDeptId(), sysDepts);

        }
    }

    @Override
    public List<SysDept> getListByParentId(String parentId) {
        EntityWrapper<SysDept> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        if(parentId!=null) {
            wrapper.eq("PARENT_ID", parentId);
        }
        wrapper.orderBy("ORDER_NUM", true);
        wrapper.orderBy("CREATE_TIME", false);

        List<SysDept> sysDepts = sysDeptService.selectList(wrapper);
        return sysDepts;
    }
}
